package com.example.smartcity_poverty.ui.AllService.RubbishClassify.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcSeekAllModel
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcSeekAllTitleModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_seek_show_list.*
import kotlinx.android.synthetic.main.re_rc_seek_show_all_title.*
import kotlinx.android.synthetic.main.re_rc_seek_show_list.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * 搜索结果实现
 */
class SeekShowListActivity : AppCompatActivity() {

    private lateinit var content: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_seek_show_list)
        title = "搜索"

        rcSeekShowEditText.addTextChangedListener {
            CoroutineScope(Dispatchers.IO).launch {
                api.getRcSeekAll(getToken(), rcSeekShowEditText.text.toString()).apply {
                    // 判断是否获取到数据
                    if (this.code == 200){
                        val rows = this.rows
                        // 填充数据
                        runOnUiThread {
                            rcSeekShowAll.apply {
                                this.layoutManager = GridLayoutManager(context, 1)
                                this.adapter = ShowSeekListAdapter(ArrayList<RcSeekAllModel.RowsBean>().apply {
                                    this.addAll(rows!!)
                                }, R.layout.re_rc_seek_show_list)
                                this.adapter?.notifyItemChanged(rows?.size!!)
                            }
                        }
                    }else runOnUiThread { this.msg?.show() }
                }
            }
            false
        }
        // 加载网络数据
        add()

        content = intent.getStringExtra("content").toString()
        rcSeekShowEditText.setText(content)

    }

    /**
     * 协程统一
     */
    private fun add() = CoroutineScope(Dispatchers.IO).launch {
        // 搜索热搜
        setRcSeekAllTitle()


        // 查找垃圾
        setRcSeekAll(content)
    }

    /**
     * 搜索垃圾分类
     */
    private suspend fun setRcSeekAll(content: String) {
        api.getRcSeekAll(getToken(), content).apply {
            // 判断是否获取到数据
            if (this.code == 200){
                val rows = this.rows
                // 填充数据
                runOnUiThread {
                    rcSeekShowAll.apply {
                        this.layoutManager = GridLayoutManager(context, 1)
                        this.adapter = ShowSeekListAdapter(ArrayList<RcSeekAllModel.RowsBean>().apply {
                            this.addAll(rows!!)
                        }, R.layout.re_rc_seek_show_list)
                    }
                }
            }else runOnUiThread { this.msg?.show() }
        }
    }


    /**
     * 热搜列表
     */
    private suspend fun setRcSeekAllTitle() {
        api.getRcSeekAllTitle(getToken()).apply {
            // 判断是否获取到数据
            if (this.code == 200){
                runOnUiThread {
                    val data = this.data
                    // 填充数据
                    rcSeekShowAllTitle.apply {
                        this.layoutManager = GridLayoutManager(context, 5)
                        this.adapter = RcSeekAllTitleAdapter(ArrayList<RcSeekAllTitleModel.DataBean>().apply {
                            this.addAll(data!!)
                        }, R.layout.re_rc_seek_show_all_title)
                    }
                }
            }else runOnUiThread { this.msg?.show()}
        }
    }

    // 热搜适配器
    inner class RcSeekAllTitleAdapter(
        val list: MutableList<RcSeekAllTitleModel.DataBean>,
        layoutId: Int
    ) : BaseRecyclerAdapter<RcSeekAllTitleModel.DataBean>(list, layoutId) {
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: RcSeekAllTitleModel.DataBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val fv = holder.viewFind
            val con = holder.viewFind.view?.context

            // 填充数据
            fv.rcSeekShowTitle.text = d.keyword

            holder.itemView.setOnClickListener {
                runOnUiThread {
                    rcSeekShowEditText.setText(d.keyword)
                }
            }
        }

        // 显示10个热搜
        override fun getItemCount(): Int = 10
    }


    /**
     * 查找结果列表适配器
     */
    inner class ShowSeekListAdapter(
      val list: MutableList<RcSeekAllModel.RowsBean>,
      layoutId: Int
    ) : BaseRecyclerAdapter<RcSeekAllModel.RowsBean>(list, layoutId){
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: RcSeekAllModel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val fv = holder.viewFind
            val con = holder.viewFind.view?.context

            glide(url + d.imgUrl, fv.rcSeekShowListImage)
            fv.rcSeekShowListTitle.text = d.name
        }

    }



    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}